/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef RECITEANALYSISDIALOG_H
#define RECITEANALYSISDIALOG_H

#include <QtGui/QWidget>
#include <QStandardItemModel>
#include <QHeaderView>
#include <QStandardItem>
#include <QDate>
#include <QList>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include <QMessageBox>
#include <QModelIndexList>
#include <QShortcut>
#include <QDialog>

#include <iostream>
#include <vector>
using namespace std;

#include "ui_ReciteAnalysisDialog.h"

#include "Options.h"
#include "WordManager.h"
#include "MessageBox.h"
#include "SQLiteDB.h"
#include "ReciteAnalysisPlot.h"

//make sure you recite a word no more than TEST_RES_COUNT_RATE/TEST_RES_INT_RATE times
#define TEST_RES_COUNT_RATE 100000
#define TEST_RES_INT_RATE 10

//make sure you recite a word no more than WORD_DES_INT_RATE times
#define WORD_DES_INT_RATE 100000

enum TestResultType
{
	TRT_Pass_First = 0,
	TRT_Fail_First,
	TRT_Pass_Last,
	TRT_Fail_Last
};

class TestResults
{
private:
	map<long, vector<int>*> mResultGroup;
	map<long, float> mPassFirstTestRate;
	map<long, float> mPassLastTestRate;

public:
	TestResults();

	void reset();
	void fitCurve();

	void addResult(int reciteCount, int interval, TestResultType testResult, int wordID);
	bool getWordIDs(int reciteCount, int interval, TestResultType testResult, vector<int>& wordIDs);
	int getWordCount(int reciteCount, int interval, TestResultType testResult);
};

class ScanRange
{
public:
	QString name;

	int redBibleBeginList;
	int redBibleEndList;

	ScanRange(QString name);
};

class SelectPolicy
{
public:
	QString name;

	int difficultyLevelMax;
	int difficultyLevelMin;
	int activeFinishedWordCountMax;
	int activeRecitingWordsCountMax;
	int activeUntouchedWordsCountMax;

	SelectPolicy(QString name);
};

class ReciteAnalysisDialog : public QDialog
{
    Q_OBJECT

public:
    ReciteAnalysisDialog(QDialog *parent = 0);
    ~ReciteAnalysisDialog();

    void showThisDialog();
    //void saveGrossPassRateMFile();
    void saveDifficultyLevelMFile();
    void saveReciteDateMFile();
    void saveWordRecitProgressMFile();

public slots:
    void scan();
    void setScanResultView();
    void showGraph();

	void createDefaultSelectPolicyFile();
	void createDefaultScanRangeFile();
	void loadSelectPolicies();
	void loadScanRange();
	void selectPolicyChanged(int);
	void scanRangeChanged(int);
	void selectWordsToRecite();
	void quickSelectDifficultWords();
	void setTask(WordManagerTaskType type);
	void recite();
	void browse();

	void selectRowAsCandidates();
	void removeRowAsCandidates();
	void clearAllRowCandidates();

private:
    Ui::ReciteAnalysisDialogClass ui;

    ReciteAnalysisPlot* mReciteAnalysisPlot;
    QStandardItemModel* mWordDateModel;
    QStandardItemModel* mCandidatesModel;

    vector<int> mRecitingWordIDs;
	vector<int> mNewWordIDs;
	vector<int> mFinishedWordIDs;
	map<long, int> mFinishedWordGroupCount;//mFinishedWordCounts<long descriptor, int wordCount>

	int mCurSelectPolicy;
	vector<SelectPolicy> mSelectPolicies;
	vector<ScanRange> mScanRanges;
	vector<int> mFinalIDs;

	TestResults* mTestResult;

	map<QDate, int> mReviewDateCount;
	map<QDate, int> mReciteNewDateCount;
	map<int, int> mDiffLevelDist;
	map<int,int> mWordRecitProgress; //map<recite count, word count>

	//map<int, vector<int> > mQuickSelectWords; //map<difficultyLevel,vector<id>>

signals:
	void reciteSelectedWords();
	void browseSelectedWords();
};

#endif // RECITEANALYSISDIALOG_H
